﻿namespace Pub.Class
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Text;

    public static class StringBuilderExtensions
    {
        public static void Clear(this StringBuilder sb)
        {
            sb.Remove(0, sb.Length);
        }

        public static StringBuilder GetBaiduSitemap(this StringBuilder code, string title, string link, string description, string text, string image, string keywords, string category, string author, string source, string pubDate)
        {
            code.Append("\t<item>" + Environment.NewLine);
            code.Append("\t\t<title>" + title.ShowXmlHtml() + "</title>" + Environment.NewLine);
            code.Append("\t\t<link>" + link.ShowXmlHtml() + "</link>" + Environment.NewLine);
            if (!description.Equals(""))
            {
                code.Append("\t\t<description>" + description.ShowXmlHtml() + "</description>" + Environment.NewLine);
            }
            if (!text.Equals(""))
            {
                code.Append("\t\t<text>" + text.ShowXmlHtml() + "</text>" + Environment.NewLine);
            }
            if (!image.Equals(""))
            {
                code.Append("\t\t<image>" + image.ShowXmlHtml() + "</image>" + Environment.NewLine);
            }
            if (!keywords.Equals(""))
            {
                code.Append("\t\t<keywords>" + keywords.ShowXmlHtml() + "</keywords>" + Environment.NewLine);
            }
            if (!category.Equals(""))
            {
                code.Append("\t\t<category>" + category.ShowXmlHtml() + "</category>" + Environment.NewLine);
            }
            if (!author.Equals(""))
            {
                code.Append("\t\t<author>" + author.ShowXmlHtml() + "</author>" + Environment.NewLine);
            }
            if (!source.Equals(""))
            {
                code.Append("\t\t<source>" + source.ShowXmlHtml() + "</source>" + Environment.NewLine);
            }
            if (!pubDate.Equals(""))
            {
                code.Append("\t\t<pubDate>" + pubDate + "</pubDate>" + Environment.NewLine);
            }
            code.Append("\t</item>" + Environment.NewLine);
            return code;
        }

        public static StringBuilder GetGoogleSitemap(this StringBuilder code, string loc)
        {
            code.Append("\t<url>" + Environment.NewLine);
            code.Append("\t\t<loc>" + loc + "</loc>" + Environment.NewLine);
            code.Append("\t\t<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" + Environment.NewLine);
            code.Append("\t\t<changefreq>daily</changefreq>" + Environment.NewLine);
            code.Append("\t\t<priority>0.6</priority>" + Environment.NewLine);
            code.Append("\t</url>" + Environment.NewLine);
            return code;
        }

        public static StringBuilder GetGoogleSitemap(this StringBuilder code, string loc, string changefreq, string priority)
        {
            code.Append("\t<url>" + Environment.NewLine);
            code.Append("\t\t<loc>" + loc + "</loc>" + Environment.NewLine);
            code.Append("\t\t<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" + Environment.NewLine);
            code.Append("\t\t<changefreq>" + changefreq + "</changefreq>" + Environment.NewLine);
            code.Append("\t\t<priority>" + priority + "</priority>" + Environment.NewLine);
            code.Append("\t</url>" + Environment.NewLine);
            return code;
        }

        public static bool IsNullEmpty(this StringBuilder sb)
        {
            return (sb.IsNull() || (sb.Length == 0));
        }

        public static void RemoveLastChar(this StringBuilder sb)
        {
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - 1, 1);
            }
        }

        public static void RemoveLastChar(this StringBuilder sb, int len)
        {
            if (sb.Length >= len)
            {
                sb.Remove(sb.Length - len, len);
            }
        }

        public static void RemoveLastChar(this StringBuilder sb, string lastchar)
        {
            if ((!sb.IsNull() && (sb.Length != 0)) && !lastchar.IsNull())
            {
                int length = lastchar.Length;
                if (sb.ToString().EndsWith(lastchar))
                {
                    sb.Remove(sb.Length - length, length);
                }
            }
        }
    }
}

